﻿using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

namespace Nirvana
{
    /// <summary>
    /// 资源导入处理器
    /// </summary>
    public sealed class AssetImportPostprocessor : AssetPostprocessor
    {
        private ModelImportRule[] modelRules;
        private AudioImportRule[] audioRules;
        private SpriteImportRule[] spriteRules;
        private TextureImportRule[] textureRules;
        private static AssetBundleNameImportRule[] assetBundleRule;

        private AudioImportRule[] GetAllAudioImportRule()
        {
            if (audioRules == null) audioRules = AudioImportRule.GetAllAssetImportRule();
            return audioRules;
        }

        private ModelImportRule[] GetAllModelImportRule()
        {
            if (modelRules == null) modelRules = ModelImportRule.GetAllAssetImportRule();
            return modelRules;
        }

        private SpriteImportRule[] GetAllSpriteImportRule()
        {
            if (spriteRules == null) spriteRules = SpriteImportRule.GetAllAssetImportRule();
            return spriteRules;
        }

        private TextureImportRule[] GetAllTextureImportRule()
        {
            if (textureRules == null) textureRules = TextureImportRule.GetAllAssetImportRule();
            return textureRules;
        }

        private static AssetBundleNameImportRule[] GetAllAssetBundleImportRule()
        {
            if (assetBundleRule == null) assetBundleRule = AssetBundleNameImportRule.GetAllAssetImportRule();
            return assetBundleRule;
        }
        

        /// <summary>
        /// 在导入音频之前调用
        /// </summary>
        private void OnPostprocessAudio(AudioClip clip)
        {
            var importer = this.assetImporter as AudioImporter;
            foreach (var p in this.GetAllAudioImportRule())
            {
                if (p.IsMatchRegex(this.assetPath))
                {
                    if (clip.length > 5)//超过5s的背景音
                    {
                        p.Match5sAudio(importer);
                    }
                    else if (clip.length > 1)//1~5中音
                    {
                        p.Match2sAudio(importer);
                    }
                    else//1s 短音
                    {
                        p.Match1sAudio(importer);
                    }
                }
            }
        }


        /// <summary>
        /// 导入模型（.fbx、.mb 文件等）中的动画之前获取通知。
        /// </summary>
        private void OnPreprocessAnimation()
        {
            
        }

        /// <summary>
        /// 导入模型（.fbx、.mb 文件等）中的动画之后获取通知。
        /// </summary>
        private void OnPostprocessAnimation(GameObject go,AnimationClip clip)
        {
            var importer = this.assetImporter as ModelImporter;
            foreach (var p in this.GetAllModelImportRule())
            {
                if (p.IsMatchRegex(this.assetPath))
                {
                    p.MatchAnimation(clip);
                }
            }
        }
        void OnPostprocessModel(GameObject g)
        {
            var importer = this.assetImporter as ModelImporter;

            foreach (var p in this.GetAllModelImportRule())
            {
                if (p.IsMatchRegex(this.assetPath))
                {
                    p.MatchModel(g);
                }
            }
        }

        /// <summary>
        /// 在导入模型之前调用
        /// </summary>
        private void OnPreprocessModel()
        {
            var importer = this.assetImporter as ModelImporter;

            foreach (var p in this.GetAllModelImportRule())
            {
                if (p.IsMatchRegex(this.assetPath))
                {
                    p.MatchModel(importer);
                }
            }
        }
        /// <summary>
        /// 在纹理导入器运行之前获取通知
        /// </summary>
        private void OnPreprocessTexture()
        {
            bool isSprite = false;
            var importer = this.assetImporter as TextureImporter;
            foreach (var p in this.GetAllSpriteImportRule())
            {
                if (p.IsMatchRegex(this.assetPath))
                {
                    isSprite = true;
                    p.MatchRule(importer);
                }
            }

            if (isSprite) return;

            foreach (var p in this.GetAllTextureImportRule())
            {
                if (p.IsMatchRegex(this.assetPath))
                {
                    p.MatchRule(importer);
                }
            }
        }
		
		private void OnPostprocessTexture(Texture2D texture)
        {

            foreach (var p in this.GetAllTextureImportRule())
            {
                if (p.IsMatchRegex(this.assetPath))
                {
                    p.FixImageNot4Rate(this.assetImporter as TextureImporter,texture);
                }
            }
        }

        //在完成任意数量的资源导入后（当资源进度条到达末尾时）调用此函数,
        //所有资源的导入，删除，移动操作都会调用该方法
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {

            foreach (var assetPath in importedAssets)
            {
                if (GetAllAssetBundleImportRule() != null)
                {
                    foreach (var p in GetAllAssetBundleImportRule())
                    {
                        if (p.IsMatchRegex(assetPath))
                        {
                            p.MarkAssetBundle(assetPath);
                        }
                    }
                }

            }

            foreach (var movedAsset in movedAssets)
            {
                if (GetAllAssetBundleImportRule() != null)
                {
                    foreach (var p in GetAllAssetBundleImportRule())
                    {
                        if (p.IsMatchRegex(movedAsset))
                        {
                            p.MarkAssetBundle(movedAsset);
                        }
                    }
                }
            }


        }

    }
}

